package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpModifyCompanyDAO;
import org.jsola.hr.dto.EmpModifyCompanyAddDTO;
import org.jsola.hr.dto.EmpModifyCompanyUpdateDTO;
import org.jsola.hr.entity.EmpModifyCompanyDO;
import org.jsola.hr.query.EmpModifyCompanyQuery;
import org.jsola.hr.service.IEmpModifyCompanyService;
import org.jsola.hr.vo.EmpModifyCompanyListVO;
import org.jsola.hr.vo.EmpModifyCompanyVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工更换用工单位
 *
 * @author zhr
 */
@Service("hrEmpModifyCompanyServiceImpl")
public class EmpModifyCompanyServiceImpl implements IEmpModifyCompanyService {

    @Autowired
    private IEmpModifyCompanyDAO empModifyCompanyDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpModifyCompanyVO save(EmpModifyCompanyAddDTO empModifyCompanyAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpModifyCompanyDO empModifyCompanyDO = empModifyCompanyAddDTO.to(EmpModifyCompanyDO.class);
        // 保存
        empModifyCompanyDO = save(empModifyCompanyDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empModifyCompanyDO.to(EmpModifyCompanyVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpModifyCompanyUpdateDTO empModifyCompanyUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpModifyCompanyDO empModifyCompanyDO = empModifyCompanyUpdateDTO.to(EmpModifyCompanyDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empModifyCompanyDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empModifyCompanyIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empModifyCompanyIds);
    }


    @Override
    public EmpModifyCompanyVO selectById(String empModifyCompanyId, String siteId) {
        EmpModifyCompanyDO empModifyCompanyDO = selectDOById(empModifyCompanyId, siteId);
        if (empModifyCompanyDO == null) {
            return null;
        }
        return empModifyCompanyDO.to(EmpModifyCompanyVO.class);
    }

    @Override
    public List<EmpModifyCompanyListVO> select(EmpModifyCompanyQuery empModifyCompanyQuery, String siteId) {
        List<EmpModifyCompanyDO> empModifyCompanyDOList = selectDO(empModifyCompanyQuery, siteId);
        if(CollectionUtils.isEmpty(empModifyCompanyDOList)) {
            return empModifyCompanyDOList == null ? null : new ArrayList<>();
        }
        return empModifyCompanyDOList.stream()
                .map(empModifyCompanyDO -> empModifyCompanyDO.to(EmpModifyCompanyListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpModifyCompanyQuery empModifyCompanyQuery, String siteId) {
        Example example = buildExample(empModifyCompanyQuery,siteId);
        return empModifyCompanyDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpModifyCompanyListVO> selectPage(EmpModifyCompanyQuery empModifyCompanyQuery, String siteId) {
        Example example = buildExample(empModifyCompanyQuery,siteId);
        Page<EmpModifyCompanyDO> page = empModifyCompanyDAO.selectPageByExample(example,
                empModifyCompanyQuery.getPageNo(),
                empModifyCompanyQuery.getPageSize());

        return page.to(EmpModifyCompanyListVO.class);
    }

    @Override
    public EmpModifyCompanyDO selectDOById(String empModifyCompanyId, String siteId) {
        return listById(empModifyCompanyId, siteId);
    }

    @Override
    public List<EmpModifyCompanyDO> selectDO(EmpModifyCompanyQuery empModifyCompanyQuery, String siteId) {
        Example example = buildExample(empModifyCompanyQuery,siteId);
        return empModifyCompanyDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param empModifyCompanyQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpModifyCompanyQuery empModifyCompanyQuery, String siteId) {
        Example example = new Example(EmpModifyCompanyDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,empModifyCompanyQuery.getOrders());
        return example;
    }
}




